# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from models import *
import operator


# Create your views here.

# 性能测试启动数据_国内
def startup(req):
    active_startup = "active"
    data_android = StartOver.objects.filter(device=0, app=1)
    data_ios = StartOver.objects.filter(device=1, app=1)
    if len(data_android.values()) == 0:
        data_android = ""
    if len(data_ios.values()) == 0:
        data_ios = ""
    return render(req, '../templates/performance/startup.html', locals())


# 性能测试启动数据_国际
def startup_inter(req):
    active_startup_inter = "active"
    data_android = StartOver.objects.filter(device=0, app=2)
    data_ios = StartOver.objects.filter(device=1, app=2)
    if len(data_android.values()) == 0:
        data_android = ""
    if len(data_ios.values()) == 0:
        data_ios = ""
    return render(req, '../templates/performance/startup_inter.html', locals())


def sql_cpu(app, sql):
    import MySQLdb as mdb
    try:
        con = mdb.connect('10.9.128.47', 'root', 'qatest', 'qa', charset='utf8');
        with con:
            cur = con.cursor()
            # 执行一个查询
            cur.execute("select DISTINCT `case` from %s WHERE app = %d" % (sql, app))
            # 取得上个查询的结果，是单个结果
            result = cur.fetchall()
            result_list = []
            for res in result:
                result_list.append(res[0])
            return result_list
    finally:
        con.close()


# cpu_国内
def cpu(req):
    sql_alabel = sql_cpu(1, 'qa_interface_appcpu')
    label_android = req.GET.get('label_android', sql_alabel[1])
    label_ios = req.GET.get('label_ios', sql_alabel[1])
    cpu_home = "active"
    data_android = AppCpu.objects.filter(case=label_android, app=1, device=0)
    data_ios = AppCpu.objects.filter(case=label_ios, app=1, device=1)
    data_list_android = []
    data_list_ios = []
    for value in data_android.values():
        value['versions'] = value['version']
        version = int(''.join(value['version'].split('.')))
        value['version'] = version
        data_list_android.append(value)
    for values in data_ios.values():
        values['versions'] = values['version']
        version = int(''.join(values['version'].split('.')))
        values['version'] = version
        data_list_ios.append(values)
    data_list_android.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_android) != 0:
        first_data_android = data_list_android[0]
        second_data_android = data_list_android[1]
        licensed1 = eval(first_data_android['detail'])
        sorned1 = eval(second_data_android['detail'])
        version01 = first_data_android['version']
        version02 = second_data_android['version']

    data_list_ios.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_ios) != 0:
        first_data_ios = data_list_ios[0]
        second_data_ios = data_list_ios[1]
        licensed2 = eval(first_data_ios['detail'])
        sorned2 = eval(second_data_ios['detail'])
        version01_ios = first_data_ios['version']
        version02_ios = second_data_ios['version']
    return render(req, '../templates/performance/cpu.html', locals())


# cpu_国际
def cpu_inter(req):
    sql_alabel = sql_cpu(2, 'qa_interface_appcpu')
    label_android = req.GET.get('label_android', sql_alabel[0])
    label_ios = req.GET.get('label_ios', sql_alabel[0])
    cpu_inter = "active"
    data_android = AppCpu.objects.filter(case=label_android, app=2, device=0)
    data_ios = AppCpu.objects.filter(case=label_ios, app=2, device=1)
    data_list_android = []
    data_list_ios = []
    for value in data_android.values():
        value['versions'] = value['version']
        version = int(''.join(value['version'].split('.')))
        value['version'] = version
        data_list_android.append(value)
    for values in data_ios.values():
        values['versions'] = values['version']
        version = int(''.join(values['version'].split('.')))
        values['version'] = version
        data_list_ios.append(values)
    data_list_android.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_android) != 0:
        first_data_android = data_list_android[0]
        second_data_android = data_list_android[1]
        licensed1 = eval(first_data_android['detail'])
        sorned1 = eval(second_data_android['detail'])
        version01 = first_data_android['version']
        version02 = second_data_android['version']

    data_list_ios.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_ios) != 0:
        first_data_ios = data_list_ios[0]
        second_data_ios = data_list_ios[1]
        licensed2 = eval(first_data_ios['detail'])
        sorned2 = eval(second_data_ios['detail'])
        version01_ios = first_data_ios['version']
        version02_ios = second_data_ios['version']
    return render(req, '../templates/performance/cpu_inter.html', locals())


def memory(req):
    memory_home = "active"
    sql_alabel = sql_cpu(1, 'qa_interface_appmemory')
    label_android = req.GET.get('label_android', sql_alabel[1])
    label_ios = req.GET.get('label_ios', sql_alabel[1])
    data_android = AppMemory.objects.filter(case=label_android, app=1, device=0)
    data_ios = AppMemory.objects.filter(case=label_ios, app=1, device=1)
    data_list_android = []
    data_list_ios = []
    for value in data_android.values():
        value['versions'] = value['version']
        version = int(''.join(value['version'].split('.')))
        value['version'] = version
        data_list_android.append(value)
    for values in data_ios.values():
        values['versions'] = values['version']
        version = int(''.join(values['version'].split('.')))
        values['version'] = version
        data_list_ios.append(values)
    data_list_android.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_android) != 0:
        first_data_android = data_list_android[0]
        second_data_android = data_list_android[1]
        licensed1 = eval(first_data_android['detail'])
        sorned1 = eval(second_data_android['detail'])
        version01 = first_data_android['version']
        version02 = second_data_android['version']

    data_list_ios.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_ios) != 0:
        first_data_ios = data_list_ios[0]
        second_data_ios = data_list_ios[1]
        licensed2 = eval(first_data_ios['detail'])
        sorned2 = eval(second_data_ios['detail'])
        version01_ios = first_data_ios['version']
        version02_ios = second_data_ios['version']
    return render(req, '../templates/performance/memory.html', locals())


def memory_inter(req):
    memory_inter = "active"
    sql_alabel = sql_cpu(2, 'qa_interface_appmemory')
    label_android = req.GET.get('label_android', sql_alabel[0])
    label_ios = req.GET.get('label_ios', sql_alabel[0])
    data_android = AppMemory.objects.filter(case=label_android, app=2, device=0)
    data_ios = AppMemory.objects.filter(case=label_ios, app=2, device=1)
    data_list_android = []
    data_list_ios = []
    for value in data_android.values():
        value['versions'] = value['version']
        version = int(''.join(value['version'].split('.')))
        value['version'] = version
        data_list_android.append(value)
    for values in data_ios.values():
        values['versions'] = values['version']
        version = int(''.join(values['version'].split('.')))
        values['version'] = version
        data_list_ios.append(values)
    data_list_android.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_android) != 0:
        first_data_android = data_list_android[0]
        second_data_android = data_list_android[1]
        licensed1 = eval(first_data_android['detail'])
        sorned1 = eval(second_data_android['detail'])
        version01 = first_data_android['version']
        version02 = second_data_android['version']

    data_list_ios.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_ios) != 0:
        first_data_ios = data_list_ios[0]
        second_data_ios = data_list_ios[1]
        licensed2 = eval(first_data_ios['detail'])
        sorned2 = eval(second_data_ios['detail'])
        version01_ios = first_data_ios['version']
        version02_ios = second_data_ios['version']
    return render(req, '../templates/performance/memory_inter.html', locals())


def electricity(req):
    electricity_home = "active"
    sql_alabel = sql_cpu(1, 'qa_interface_appelectricity')
    label_android = req.GET.get('label_android', sql_alabel[1])
    label_ios = req.GET.get('label_ios', sql_alabel[1])
    data_android = AppElectricity.objects.filter(case=label_android, app=1, device=0)
    data_ios = AppElectricity.objects.filter(case=label_ios, app=1, device=1)
    data_list_android = []
    data_list_ios = []
    for value in data_android.values():
        value['versions'] = value['version']
        version = int(''.join(value['version'].split('.')))
        value['version'] = version
        data_list_android.append(value)
    for values in data_ios.values():
        values['versions'] = values['version']
        version = int(''.join(values['version'].split('.')))
        values['version'] = version
        data_list_ios.append(values)
    data_list_android.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_android) != 0:
        first_data_android = data_list_android[0]
        second_data_android = data_list_android[1]
        licensed1 = eval(first_data_android['detail'])
        sorned1 = eval(second_data_android['detail'])
        version01 = first_data_android['version']
        version02 = second_data_android['version']

    data_list_ios.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_ios) != 0:
        first_data_ios = data_list_ios[0]
        second_data_ios = data_list_ios[1]
        licensed2 = eval(first_data_ios['detail'])
        sorned2 = eval(second_data_ios['detail'])
        version01_ios = first_data_ios['version']
        version02_ios = second_data_ios['version']
    return render(req, '../templates/performance/electricity.html', locals())


def electricity_inter(req):
    electricity_inter = "active"
    sql_alabel = sql_cpu(2, 'qa_interface_appelectricity')
    label_android = req.GET.get('label_android', sql_alabel[0])
    label_ios = req.GET.get('label_ios', sql_alabel[0])
    data_android = AppElectricity.objects.filter(case=label_android, app=2, device=0)
    data_ios = AppElectricity.objects.filter(case=label_ios, app=2, device=1)
    data_list_android = []
    data_list_ios = []
    for value in data_android.values():
        value['versions'] = value['version']
        version = int(''.join(value['version'].split('.')))
        value['version'] = version
        data_list_android.append(value)
    for values in data_ios.values():
        values['versions'] = values['version']
        version = int(''.join(values['version'].split('.')))
        values['version'] = version
        data_list_ios.append(values)
    data_list_android.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_android) != 0:
        first_data_android = data_list_android[0]
        second_data_android = data_list_android[1]
        licensed1 = eval(first_data_android['detail'])
        sorned1 = eval(second_data_android['detail'])
        version01 = first_data_android['version']
        version02 = second_data_android['version']

    data_list_ios.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_ios) != 0:
        first_data_ios = data_list_ios[0]
        second_data_ios = data_list_ios[1]
        licensed2 = eval(first_data_ios['detail'])
        sorned2 = eval(second_data_ios['detail'])
        version01_ios = first_data_ios['version']
        version02_ios = second_data_ios['version']
    return render(req, '../templates/performance/electricity_inter.html', locals())


def flow(req):
    flow_home = "active"
    sql_alabel = sql_cpu(1, 'qa_interface_appflow')
    label_android = req.GET.get('label_android', sql_alabel[1])
    label_ios = req.GET.get('label_ios', sql_alabel[1])
    data_android = AppFlow.objects.filter(case=label_android, app=1, device=0)
    data_ios = AppFlow.objects.filter(case=label_ios, app=1, device=1)
    data_list_android = []
    data_list_ios = []
    for value in data_android.values():
        value['versions'] = value['version']
        version = int(''.join(value['version'].split('.')))
        value['version'] = version
        value['detail'] = eval(value['detail'])['download']
        data_list_android.append(value)
    for values in data_ios.values():
        values['versions'] = values['version']
        version = int(''.join(values['version'].split('.')))
        values['version'] = version
        values['detail'] = eval(values['detail'])['download']
        data_list_ios.append(values)
    data_list_android.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_android) != 0:
        first_data_android = data_list_android[0]
        second_data_android = data_list_android[1]
        licensed1 = first_data_android['detail']
        sorned1 = second_data_android['detail']
        version01 = first_data_android['version']
        version02 = second_data_android['version']

    data_list_ios.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_ios) != 0:
        first_data_ios = data_list_ios[0]
        second_data_ios = data_list_ios[1]
        licensed2 = first_data_ios['detail']
        sorned2 = second_data_ios['detail']
        version01_ios = first_data_ios['version']
        version02_ios = second_data_ios['version']
    return render(req, '../templates/performance/flow.html', locals())


def flow_inter(req):
    flow_inter = "active"
    sql_alabel = sql_cpu(2, 'qa_interface_appflow')
    label_android = req.GET.get('label_android', sql_alabel[0])
    label_ios = req.GET.get('label_ios', sql_alabel[0])
    data_android = AppFlow.objects.filter(case=label_android, app=2, device=0)
    data_ios = AppFlow.objects.filter(case=label_ios, app=2, device=1)
    data_list_android = []
    data_list_ios = []
    for value in data_android.values():
        value['versions'] = value['version']
        version = int(''.join(value['version'].split('.')))
        value['version'] = version
        value['detail'] = eval(value['detail'])['download']
        data_list_android.append(value)
    for values in data_ios.values():
        values['versions'] = values['version']
        version = int(''.join(values['version'].split('.')))
        values['version'] = version
        values['detail'] = eval(values['detail'])['download']
        data_list_ios.append(values)
    data_list_android.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_android) != 0:
        first_data_android = data_list_android[0]
        second_data_android = data_list_android[1]
        licensed1 = first_data_android['detail']
        sorned1 = second_data_android['detail']
        version01 = first_data_android['version']
        version02 = second_data_android['version']
    data_list_ios.sort(key=operator.itemgetter('version'), reverse=True)
    if len(data_list_ios) != 0:
        first_data_ios = data_list_ios[0]
        second_data_ios = data_list_ios[1]
        licensed2 = first_data_ios['detail']
        sorned2 = second_data_ios['detail']
        version01_ios = first_data_ios['version']
        version02_ios = second_data_ios['version']
    return render(req, '../templates/performance/flow_inter.html', locals())


def list_time(req):
    list_home = "active"
    return render(req, '../templates/performance/developing.html', locals())


def list_time_inter(req):
    list_inter = "active"
    return render(req, '../templates/performance/developing.html', locals())
